package lx.af.manager;

import android.app.Application;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * author: liuxu
 * date: 2015-02-12
 *
 * to easy access google Volley.
 * https://android.googlesource.com/platform/frameworks/volley
 */
public final class VolleyManager {

    private static final String TAG = Volley.class.getSimpleName();

    private static Application sApp;
    private static RequestQueue mRequestQueue;
    private static ImageLoader mImageLoader;

    private VolleyManager() {}

    /**
     * init essential objects.
     * this should be called in Application.onCreate().
     */
    public static void init(Application app) {
        sApp = app;
        mRequestQueue = Volley.newRequestQueue(sApp);
        mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache());
    }

    public static RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    public static ImageLoader getImageLoader() {
        return mImageLoader;
    }

    /**
     * start request.
     * @param request the request
     * @param tag tag for the request, can be used to cancel pending requests
     * @param <T> the request
     */
    public static <T> void addRequest(Request<T> request, String tag) {
        request.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        mRequestQueue.add(request);
    }

    /**
     * start request with a default tag.
     * @param request the request
     * @param <T> the request
     */
    public static <T> void addRequest(Request<T> request) {
        request.setTag(TAG);
        mRequestQueue.add(request);
    }

    /**
     * cancel all pending request with tag.
     * @param tag the tag
     */
    public static void cancelPendingRequests(Object tag) {
        mRequestQueue.cancelAll(tag);
    }

    /**
     * cancel all pending request with default tag:
     * all request added by addRequest() method without a tag param has a default tag.
     */
    public static void cancelPendingRequests() {
        mRequestQueue.cancelAll(TAG);
    }



    private static class LruBitmapCache extends LruCache<String, Bitmap>
            implements ImageLoader.ImageCache {

        public static int getDefaultLruCacheSize() {
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
            // threshold set to 1/8 of total available memory
            final int cacheSize = maxMemory / 8;
            return cacheSize;
        }

        public LruBitmapCache() {
            this(getDefaultLruCacheSize());
        }

        public LruBitmapCache(int sizeInKiloBytes) {
            super(sizeInKiloBytes);
        }

        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight() / 1024;
        }

        @Override
        public Bitmap getBitmap(String url) {
            return get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            put(url, bitmap);
        }
    }
}
